<template>

  <el-card class="box-card">
    <div
      slot="header"
      class="clearfix"
    >
      <span>登录</span>
      <el-button
        style="float: right; padding: 3px 0"
        type="text"
        @click="gozc"
      >去注册</el-button>
    </div>
    <!--  -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
    >
      <el-form-item
        label="用户名"
        prop="loginName"
      >
        <el-input v-model="ruleForm.loginName"></el-input>
      </el-form-item>

      <el-form-item
        label="密码"
        prop="loginKey"
      >
        <el-input type="password" v-model="ruleForm.loginKey"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
        >提交</el-button>
        <el-button>重置</el-button>
      </el-form-item>

    </el-form>
  </el-card>

</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapMutations, mapActions } = createNamespacedHelpers("login");

export default {
  data() {
    return {
      ruleForm: {
        loginName: "",
        loginKey: ""
      },
      rules: {
        loginName: [
          { required: true, message: "请输入用户名", trigger: "blur" }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        loginKey: [
          { required: true, message: "请输入密码", trigger: "blur" }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    ...mapActions(["getLogin"]),
    gozc() {
      this.$router.push("/reg");
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.getLogin(this.ruleForm)
            .then(() => {
              this.$message({
                message: "登录成功",
                type: "success"
              });
            })
            .then(() => {
              this.$router.push("/main");
            });
        } else {
          alter("登录失败");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.box-card {
  margin: 50px auto;
}
</style>
